package com.qdlc.p2p.biz.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.CollectionUtils;

import com.qdlc.p2p.biz.processor.event.NoticeTemplateValueEvent;
import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.core.queue.QueueHandlers;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.common.NoticeTypeHelper;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.common.NoticeTypeHelper.NidType;
import com.qdlc.p2p.dal.common.SystemConfigHelper.Nid;
import com.qdlc.p2p.dal.dto.User;

/**
 * 通知模版内容发送器，指定发送的通知nid和通知类型，发送通知模版内容
 * 可以发送短信、邮件、站内信
 *
 * @author linya
 */
public class NoticeTemplateSender {

    //发送给的用户
    private User user;

    //通知nid
    private String noticeTypeNid;

    //指定特定的通知类型，也可以不指定，如果不指定，则发送存在的所有通知类型
    private NidType nidType;

    //通知模版内容需要补充的占位符数据
    private Map<String, Object> sendData = new HashMap<String, Object>();

    private NoticeTemplateSender(User user, String noticeTypeNid, NidType nidType) {
        this.user = user;
        this.noticeTypeNid = noticeTypeNid;
        this.nidType = nidType;
        initNoticeTemplateCommonSendData();
    }

    public static NoticeTemplateSender newInstance(User user, String noticeTypeNid) {
        return newInstance(user, noticeTypeNid, null);
    }

    public static NoticeTemplateSender newInstance(User user, String nid, NidType nidType) {
        NoticeTemplateSender noticeTemplateSender = new NoticeTemplateSender(user, nid, nidType);
        return noticeTemplateSender;
    }

    /**
     * 获取需要发送的通知事件
     */
    private List<NoticeTemplateValueEvent> initNoticeTemplateValueEvent() {
        //获取通知的NidType，如果没有设置，则获取所有存在的NidType
        List<NidType> nidTypeList = null;
        if (nidType == null) {
            nidTypeList = NoticeTypeHelper.NidType.getNitTypeList();
        } else {
            nidTypeList = new ArrayList<NidType>();
            nidTypeList.add(nidType);
        }

        List<NoticeTemplateValueEvent> noticeTemplateValueEvents = new ArrayList<NoticeTemplateValueEvent>();
        for (NidType nidType : nidTypeList) {
            NoticeTemplateValueEvent event = NoticeTemplateValueEvent.newInstance(user, noticeTypeNid, nidType);
            event.addSendData(sendData);
            noticeTemplateValueEvents.add(event);
        }

        return noticeTemplateValueEvents;
    }

    /**
     * 初始化模版最基础的公用数据
     */
    private void initNoticeTemplateCommonSendData() {
        sendData.put("noticeTime", DateUtil.getTime(DateUtil.getNow()));
        sendData.put("web_url", SystemConfigHelper.getValue(Nid.WEB_URL));
        sendData.put("web_name", SystemConfigHelper.getValue(Nid.WEB_NAME));
        sendData.put("customer_hotline", SystemConfigHelper.getValue(Nid.CUSTOMER_HOTLINE));
        sendData.put("copyright", SystemConfigHelper.getValue(Nid.COPYRIGHT));
        sendData.put("web_record_number", SystemConfigHelper.getValue(Nid.WEB_RECORD_NUMBER));
        sendData.put("sendTime", DateUtil.dateStr4(DateUtil.getNow()));

        //兼容以前的传参方式
        sendData.putAll(Global.getTransfer());
    }

    public NoticeTemplateSender addSendData(Map<String, Object> sendData) {
        this.sendData.putAll(sendData);
        return this;
    }

    public NoticeTemplateSender addSendData(String key, Object value) {
        sendData.put(key, value);
        return this;
    }

    public void send() {
        //获取通知事件
        List<NoticeTemplateValueEvent> events = initNoticeTemplateValueEvent();
        if (CollectionUtils.isEmpty(events)) {
            return;
        }
        //放入队列处理
        QueueHandlers.put(events);
    }

    public void send(Map<String, Object> sendData) {
        this.sendData.putAll(sendData);
        // 获取通知事件
        List<NoticeTemplateValueEvent> events = initNoticeTemplateValueEvent();
        if (CollectionUtils.isEmpty(events)) {
            return;
        }
        //放入队列处理
        QueueHandlers.put(events);
    }
}
